using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace CincinnatiLibraryCheckAvailability
{
    [Serializable()]
    public class Library
    {
        private string name;
        private string url;
        private TimeSpan[] opensAt = new TimeSpan[7] { TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue };
        private TimeSpan[] closesAt = new TimeSpan[7] { TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue, TimeSpan.MaxValue };
        private bool check;
        private bool email;

        public Library()
        {
        }

        public Library(string name, string url, TimeSpan[] opensAt, TimeSpan[] closesAt, bool check, bool email)
        {
            this.name = name;
            this.url = url;
            if(opensAt.Length==7)
                this.opensAt = opensAt;
            if(closesAt.Length==7)
                this.closesAt = closesAt;
            this.check = check;
            this.email = email;
            
        }

        public Library(SerializationInfo info, StreamingContext ctxt)
        {
            name = (string)info.GetValue("Name", typeof(string));
            url = (string)info.GetValue("URL", typeof(string));
            opensAt = (TimeSpan[])info.GetValue("OpensAt", typeof(TimeSpan[]));
            closesAt = (TimeSpan[])info.GetValue("ClosesAt",typeof(TimeSpan[]));
            check = (bool)info.GetValue("Check", typeof(bool));
            email = (bool)info.GetValue("Email", typeof(bool));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            //You can use any custom name for your name-value pair. But make sure you
            // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
            // then you should read the same with "EmployeeId"
            info.AddValue("Name", name);
            info.AddValue("URL", url);
            info.AddValue("OpensAt", opensAt);
            info.AddValue("ClosesAt", closesAt);
            info.AddValue("Check", check);
            info.AddValue("Email", email);
        }

        public static Library FromName(string name)
        {
            return Libraries.Find(name);
        }

        public override string ToString()
        {
            return name;
        }

        public void ToggleCheck()
        {
            check = !check;
        }

        public bool IsMonitored
        {
            get { return check; }
            set { check = value; }
        }

        public bool generatesEmail
        {
            get { return email; }
            set { email = value; }
        }

        public string URL
        {
            get { return url; }
        }
        public string Name
        {
            get { return name; }
        }
        
        public bool IsOpenNow
        {
            get
            {
                int dayofweek = (int)DateTime.Today.DayOfWeek;
                if (opensAt[dayofweek] == TimeSpan.MaxValue || closesAt[dayofweek] == TimeSpan.MaxValue)
                    return true;
                TimeSpan fromMidnight = DateTime.Now - DateTime.Today;
                return fromMidnight > opensAt[dayofweek] && fromMidnight < closesAt[dayofweek];
            }
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(Library))
                return false;
            else return ((Library)obj).url == this.url && ((Library)obj).name == this.name;
        }

        public override int GetHashCode()
        {
            return url.GetHashCode();
        }
    }
}
